﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TvTipper.Entities;

namespace TvTipper
{
    public partial class ProgramEditForm : Form
    {
        public ProgramEditForm()
        {
            InitializeComponent();
        }

        public string ProgramName { get; set; }
        public string Description { get; set; }
        public int? StartYear { get; set; }
        public int? EndYear { get; set; }
        public int Seasons { get; set; }



        protected override void OnLoad(EventArgs e)
        {         
            base.OnLoad(e);
            m_name.Text = ProgramName;
            m_desc.Text = Description;
            m_season.Text = Seasons.ToString();
            m_start.Text = StartYear.HasValue ? StartYear.ToString() : "";
            m_end.Text = EndYear.HasValue ? EndYear.ToString() : ""; 
        }

        private void m_btnOk_Click(object sender, EventArgs e)
        {
            m_error.Clear();

            if (string.IsNullOrEmpty(m_name.Text))
            {
                DialogResult = DialogResult.None;
                m_error.SetError(m_name, "Name cannot be empty");
                return;
            }

            if (string.IsNullOrEmpty(m_season.Text))
            {
                Seasons = 0;
            }
            else
            {
                Seasons = Int32.Parse(m_season.Text);
            }

            int? start = null;
            if (!string.IsNullOrEmpty(m_start.Text))
            {
                start = Int32.Parse(m_start.Text);
                if (start < 1930 || start > 2020)
                {
                    DialogResult = DialogResult.None;
                    m_error.SetError(m_start, "The value is not within the valid range (1930-2020)");
                    return;
                }
            }

            int? end = null;
            if (!string.IsNullOrEmpty(m_end.Text))
            {
                end = Int32.Parse(m_end.Text);
                if (end < 1930 || end > 2020)
                {
                    DialogResult = DialogResult.None;
                    m_error.SetError(m_end, "The value is not within the valid range (1930-2020)");
                    return;
                }
            }

            if ((end.HasValue && start.HasValue) && (end.Value < start.Value))
            {
                DialogResult = DialogResult.None;
                m_error.SetError(m_start, "The end year is lower the start year");
                return;
            }

            ProgramName = m_name.Text;
            Description = m_desc.Text;
            StartYear = start;
            EndYear = end;
            Seasons = Seasons;

        }

        private void OnYearKeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar);
        }
    }
}
